#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 1e5 + 10;
int a[N];

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i];
	sort(a, a + n);
	ll ans = 0;
	for (int i = 0; i < n; i++) {
		ans += abs(a[i] - a[n / 2]);
	}
	cout << ans << endl;
	return 0;
}